﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using PowerCreator.LMS.DL.Project.Entity;
using PowerCreator.LMS.Web.DL.Project;
using PowerCreator.LMS.Common.Script;

public partial class Theme_DL_OtherUserClassTasks : PowerCreator.LMS.Web.Base.WebBasePage
{
    protected string ClassName = string.Empty;
    protected string CourseNum = string.Empty;
    private string CourseID = string.Empty;
    PowerCreator.LMS.ViewQuery.ViewQuery obj = new PowerCreator.LMS.ViewQuery.ViewQuery();
    VOTOClassTasks voClassTask = new VOTOClassTasks();
    TOClassTasks objClassTask = new TOClassTasks();
    private VOTOClass voClass = new VOTOClass();
    TOClass objClass = new TOClass();
    VOTOClassTasksReply votaskreply = new VOTOClassTasksReply();
    TOClassTasksReply objtaskReply = new TOClassTasksReply();
    PowerCreator.LMS.ViewQuery.ViewQuery ov = new PowerCreator.LMS.ViewQuery.ViewQuery();
    protected string classID = "0";
    protected string courseListID = "0";


    protected void Page_Load(object sender, EventArgs e)
    {
        int CID = 0;
        if (Request.QueryString["ClassID"] != null)
        {
            classID = Request.QueryString["ClassID"].ToString();
            CID = int.Parse(classID);
            ViewState["ClassID"] = classID;
            voClass = objClass.Load(CID);
            ClassName = voClass.Name;
            lbl_ClassName.Text = ClassName;
            BinderPG(voClass);
        }
        if (Request.QueryString["CourseListID"] != null)
        {
            courseListID = Request.QueryString["CourseListID"].ToString();
            ViewState["CourseListID"] = courseListID;
        }

    }



    private void BinderPG(VOTOClass vo)
    {
        VOTOCourseList voCourselist = new VOTOCourseList();
        TOCourseList objCourselist = new TOCourseList();
        PowerCreator.LMS.ViewQuery.ViewQuery ov = new PowerCreator.LMS.ViewQuery.ViewQuery();
        PowerCreator.LMS.Web.DL.User.LoginEntity login = new PowerCreator.LMS.Web.DL.User.LoginEntity();
        login = (PowerCreator.LMS.Web.DL.User.LoginEntity)Session[PowerCreator.LMS.Web.Config.WebConfig.SESSION_USER_USERINFO];
        if (login != null)
        {
            voCourselist = objCourselist.Load(vo.ClassCourseListID);
            string CourseIDsList = voCourselist.CourseID;
            string[] courseid = CourseIDsList.Split('#');
            CourseNum = courseid.Length.ToString();
            string courseidlist = string.Empty;
            for (int i = 0; i < courseid.Length; i++)
            {
                string[] par = courseid[i].Split(',');
                if (par.Length == 4)
                {
                    if (courseidlist.Length > 0)
                        courseidlist += ",";
                    courseidlist += par[0];
                }
            }
            if (courseidlist.Length == 0)
                courseidlist = "0";



            string search = "SELECT TO_Course.Course_Id, TO_Course.CourseName, TO_ClassTasks.ClassTasks_Id, TO_ClassTasks.ClassID, TO_ClassTasks.TasksTitle, TO_ClassTasks.EndDate, TO_ClassTasks.Broswer,TO_ClassTasks.CreatDate, TO_ClassTasks.SetTop, TO_ClassTasks.SetHighLight FROM TO_ClassTasks,TO_Course where TO_ClassTasks.Course_ID = TO_Course.Course_Id  and TO_ClassTasks.ClassID=" + ViewState["ClassID"].ToString();
            DataTable dt = obj.Query(search);



            search = "SELECT  Course_ID,ClassTasks_Id, ClassID, TasksTitle, EndDate, Broswer,CreatDate, SetTop,SetHighLight FROM TO_ClassTasks where Course_ID = 0  and ClassID=" + ViewState["ClassID"].ToString();
            DataTable dt03 = obj.Query(search);
            dt03.Columns.Add("CourseName");


            for (int k = 0; k < dt03.Rows.Count; k++)
            {
                dt03.Rows[k]["CourseName"] = "--";

                dt.Rows.Add(dt03.Rows[k]["Course_Id"], dt03.Rows[k]["CourseName"], dt03.Rows[k]["ClassTasks_Id"], dt03.Rows[k]["ClassID"], dt03.Rows[k]["TasksTitle"], dt03.Rows[k]["EndDate"], dt03.Rows[k]["Broswer"], dt03.Rows[k]["CreatDate"], dt03.Rows[k]["SetTop"], dt03.Rows[k]["SetHighLight"], dt03.Rows[k]["rowstat"]);


            }

            DataTable dt04 = new DataTable();
            if (dt.Rows.Count > 0)
            {
                search = "SELECT TO_UserClassTask.UserTask_ID, TO_UserClassTask.Creator, TO_Learner.UserName, TO_Learner.Name FROM TO_Learner INNER JOIN TO_UserClassTask ON  TO_Learner.Learner_Id = TO_UserClassTask.Creator where TO_UserClassTask.Task_Id=" + dt.Rows[0]["ClassTasks_Id"].ToString() + " and TO_UserClassTask.Creator<>" + login.UserID.ToString() + "";
                dt04 = ov.Query(search);
                dt04.Columns.Add("CourseName");
                dt04.Columns.Add("TasksTitle");
                dt04.Columns.Add("ClassTasks_Id");
                dt04.Columns.Add("Course_ID");
                dt04.Columns.Add("Score");
                for (int j = 0; j < dt04.Rows.Count; j++)
                {
                    dt04.Rows[j]["TasksTitle"] = dt.Rows[0]["TasksTitle"];
                    dt04.Rows[j]["CourseName"] = dt.Rows[0]["CourseName"];
                    dt04.Rows[j]["ClassTasks_Id"] = dt.Rows[0]["ClassTasks_Id"];
                    dt04.Rows[j]["Course_ID"] = dt.Rows[0]["Course_ID"];
                    dt04.Rows[j]["Score"] = getPerScore(dt04.Rows[j]["UserTask_ID"].ToString());
                }

                DataTable dt05 = new DataTable();
                for (int p = 1; p < dt.Rows.Count; p++)
                {
                    search = "SELECT TO_UserClassTask.UserTask_ID, TO_UserClassTask.Creator, TO_Learner.UserName, TO_Learner.Name FROM TO_Learner INNER JOIN TO_UserClassTask ON  TO_Learner.Learner_Id = TO_UserClassTask.Creator where TO_UserClassTask.Task_Id=" + dt.Rows[p]["ClassTasks_Id"].ToString() + " and TO_UserClassTask.Creator<>" + login.UserID.ToString() + "";
                    dt05 = ov.Query(search);
                    
                    for (int h = 0; h < dt05.Rows.Count; h++)
                    {
                        DataRow dr = dt04.NewRow();
                        dr["UserTask_ID"] = dt05.Rows[h]["UserTask_ID"];
                        dr["Creator"] = dt05.Rows[h]["Creator"];
                        dr["UserName"] = dt05.Rows[h]["UserName"];
                        dr["Name"] = dt05.Rows[h]["Name"];
                        dr["TasksTitle"] = dt.Rows[p]["TasksTitle"];
                        dr["CourseName"] = dt.Rows[p]["CourseName"];
                        dr["ClassTasks_Id"] = dt.Rows[p]["ClassTasks_Id"];
                        dr["Course_ID"] = dt.Rows[p]["Course_ID"];
                        dr["Score"] = getPerScore(dt05.Rows[h]["UserTask_ID"].ToString());

                        dt04.Rows.Add(dr);
                    }
                }


            }

            dt04.Columns.Add("bg");
            int iResult = 0;

            for (int k = 0; k < dt04.Rows.Count; k++)
            {
                int a = Math.DivRem(k, 2, out iResult);
                if (iResult == 0)
                    dt04.Rows[k]["bg"] = "#FFFFFF";
                if (iResult == 1)
                    dt04.Rows[k]["bg"] = "#EEEEEE";


            }
            rptNew.DataSource = dt04;
            rptNew.DataBind();

        }
    }

    private string getPerScore(string UserTask_ID)
    {
        string sql = "";
        int total = 0;
        int num = 0;
        string per = "--";
        DataTable dt = new DataTable();

        sql = " ReplyType=1 and UserTask_Id=" + UserTask_ID;
        num = objtaskReply.Count(sql);
        if (num > 0)
        {
            sql = "select sum(Point) as sum from TO_ClassTasksReply where " + sql;
            dt = ov.Query(sql);
            if (dt.Rows.Count == 1)
            {
                total = int.Parse(dt.Rows[0]["sum"].ToString());
                per = (total / num).ToString();
            }
        }

        return per;

    }
    protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            LinkButton lnkbtn_editor = (LinkButton)e.Item.FindControl("lnkbtn_editor");
            Label lab_ID = (Label)e.Item.FindControl("lab_ID");

            if (lab_ID.Text == "0")
            {
                lnkbtn_editor.Enabled = false;
            }
        }
    }




    protected void rptNew_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        VOTOClassTasks vo = new VOTOClassTasks();
        Label lbl_msg = (Label)e.Item.FindControl("lbl_msg");
        Label lbl_CourseID = (Label)e.Item.FindControl("lbl_CourseID");
        Label lab_ID = (Label)e.Item.FindControl("lab_ID");
        Label lbl_UserTask_ID = (Label)e.Item.FindControl("lbl_UserTask_ID");
      
        if (e.CommandName.ToLower() == "leditor")
        {
            string url = "UserClassTasks.aspx?UserTask_ID=" + lbl_UserTask_ID.Text + "&ClassID=" + ViewState["ClassID"].ToString() + "&CourseID=" + lbl_CourseID.Text + "&TaskID=" + e.CommandArgument.ToString();
            Script.RedirectURL(url);

        }



    }


}
